package com.weibotwo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.weibotwo.common.Exception.UserServiceException;
import com.weibotwo.common.util.FaceFormat;
import com.weibotwo.common.util.PageResult;
import com.weibotwo.dao.AdminMapper;
import com.weibotwo.dao.UserDao;
import com.weibotwo.dao.WeiboTabMapper;
import com.weibotwo.pojo.Shell;
import com.weibotwo.pojo.User;
import com.weibotwo.pojo.WeiboTabExample;
import com.weibotwo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 管理服务层
 * @ClassName AdminServiceImpl
 * @Author 天火
 * @Date 2019-03-22
 * @Version 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private WeiboTabMapper weiboTabMapper;

    @Autowired
    private UserDao userDao;

    @Override
    public Map<String, Object> showAdminIndex() {
        Map<String,Object> map = new HashMap<>();

        try {

            WeiboTabExample example = new WeiboTabExample();
            //用户数
            int u1 = adminMapper.findTargetNum("user_tab", "u_state = 0", "REGISTERTIME");
            int u2 = adminMapper.findAllNum("user_tab");
            //微博数
            int w1 = adminMapper.findTargetNum("weibo_tab", "1=1", "M_DATE");
            Long w2 = weiboTabMapper.countByExample(example);
            //海螺数
            int c1 = adminMapper.findTargetNum("shell_tab", "1=1", "shelltime");
            int c2 = adminMapper.findAllNum("shell_tab");
            //评论数
            int p1 = adminMapper.findTargetNum("comm_tab", "1=1", "C_DATE");
            int p2 = adminMapper.findAllNum("comm_tab");
            //回复数
            int h1 = adminMapper.findTargetNum("comm_tab", "C_STATE = 100 or C_STATE = 101 ", "C_DATE");
            int h2 = adminMapper.findAllAnswerNum();


            //统计最近一周七天的微博数和海螺数
            String week1 = adminMapper.findDay(6);
            String week2 = adminMapper.findDay(5);
            String week3 = adminMapper.findDay(4);
            String week4 = adminMapper.findDay(3);
            String week5 = adminMapper.findDay(2);
            String week6 = adminMapper.findDay(1);
            String week7 = adminMapper.findDay(0);

            //查询最近七天的微博和海螺数目
            int ww7 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 0);
            int ww6 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 1);
            int ww5 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 2);
            int ww4 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 3);
            int ww3 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 4);
            int ww2 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 5);
            int ww1 = adminMapper.finWeiboOrShellDay("weibo_tab", "M_DATE", 6);

            int wc1 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 6);
            int wc2 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 5);
            int wc3 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 4);
            int wc4 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 3);
            int wc5 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 2);
            int wc6 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 1);
            int wc7 = adminMapper.finWeiboOrShellDay("shell_tab", "shelltime", 0);

            map.put("u1", u1);
            map.put("u2", u2);
            map.put("w1", w1);
            map.put("w2", w2);
            map.put("c1", c1);
            map.put("c2", c2);
            map.put("p1", p1);
            map.put("p2", p2);
            map.put("h1", h1);
            map.put("h2", h2);
            map.put("week1", week1);
            map.put("week2", week2);
            map.put("week3", week3);
            map.put("week4", week4);
            map.put("week5", week5);
            map.put("week6", week6);
            map.put("week7", week7);
            map.put("ww1", ww1);
            map.put("ww2", ww2);
            map.put("ww3", ww3);
            map.put("ww4", ww4);
            map.put("ww5", ww5);
            map.put("ww6", ww6);
            map.put("ww7", ww7);
            map.put("wc7", wc7);
            map.put("wc6", wc6);
            map.put("wc5", wc5);
            map.put("wc4", wc4);
            map.put("wc3", wc3);
            map.put("wc2", wc2);
            map.put("wc1", wc1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public PageResult findAllUser(int pageNum) {
        PageResult result = new PageResult();
        try {
            PageHelper.startPage(pageNum,10);
            List<User> users = adminMapper.findAllUser();
            Page<User> userPage = (Page<User>) users;
            result.setPageTotals(userPage.getPages());
            result.setTotals((int)userPage.getTotal());
            result.setRows(userPage.getResult());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public PageResult findAllUserByKey(String key,int pageNum) {
        PageResult result = new PageResult();
        try {
            PageHelper.startPage(pageNum,10);
            List<User> users = userDao.findUserBySeach(key);
            Page<User> userPage = (Page<User>) users;
            result.setPageTotals(userPage.getPageSize());
            result.setTotals((int)userPage.getTotal());
            result.setRows(userPage.getResult());
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public PageResult findAllShellNyKey(String Key, int pageNum) {
        PageResult result = new PageResult();
        try {
            String nkey = "";
            if(Key == null){
                nkey = "1=1";
            }else{
                nkey = "SHELLBODY like '%"+Key+"%'";
            }
            PageHelper.startPage(pageNum,10);
            List<Shell> shells = adminMapper.findShellByKey(nkey);
            shells = FaceFormat.shellFormt(shells);

            Page<Shell> shellPage = (Page<Shell>) shells;
            for(Shell shell:shells){
                shell.setChUser(userDao.findUserById(shell.getUserId()));
                if(Key != null){
                    shell.setShellBody(FaceFormat.keyFormt(shell.getShellBody(),Key));
                }
            }
            result.setPageTotals(shellPage.getPages());
            result.setTotals((int)shellPage.getTotal());
            result.setRows(shellPage.getResult());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public User adminLogin(String name, String password) throws UserServiceException{
        User user = null;
        try {
            user = adminMapper.findAdminUser(name,password);
            final String banTime = user.getBanTime();
        } catch (Exception e) {
            throw new UserServiceException("用户名或密码错误！");
        }
        return user;
    }

    @Override
    public void updateWeiboState(Long wId, String operation) {
        try{
            if("delete".equals(operation)){
                adminMapper.updateWeiboStateByWId(wId, 1L);
            }else if("recover".equals(operation)){
                adminMapper.updateWeiboStateByWId(wId, 0L);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void bannedUserDaysById(Long userId, int bannedDay) {
        try{
            adminMapper.updateBanTimeByUserId(userId,bannedDay);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void unBlockUserByuerId(Long userId) {
        try{
            adminMapper.unBlockUser(userId);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void deleteAndRestoreShell(Long sId, String action) {
        try{
            /**
             * state
             * 0代表未解决
             * 1代表已解决
             * 2代表已删除未解决
             * 3代表已删除已解决
             */
            Long state = adminMapper.findShellState(sId);
            if("delete".equals(action)){
                if(state==0){
                    adminMapper.updateShellState(sId,2L);
                }else {
                    adminMapper.updateShellState(sId,3L);
                }
            }else if("restore".equals(action)){
                if(state==3){
                    adminMapper.updateShellState(sId,1L);
                }else {
                    adminMapper.updateShellState(sId,0L);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }


}
